package com.neuedu.helloworld;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;


public class MyFragment extends Fragment implements View.OnClickListener , AdapterView.OnItemClickListener {


    private Button btnBookTicket;
    private ListView lvMainInfo;
    private ArrayAdapter adapter;

    List<String> listViewData = new ArrayList<>(3);


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



        btnBookTicket =getActivity().findViewById(R.id.btnBookTicket);
        lvMainInfo = getActivity().findViewById(R.id.lvMainInfo);
        Log.d("MainActivity", ":  MainActivity.onCreate ");
        btnBookTicket.setOnClickListener(this);

        lvMainInfo.setOnItemClickListener(this);

        adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1  , listViewData );

        //通过适配器 将数据绑定到 ListView
        lvMainInfo.setAdapter(adapter);

        initData();



    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Class activityClass = null;
        switch (position){
            case 0:
                activityClass = ConcatActivity.class;
                break;
            case 1:
                break;
            case 2:
                break;
        }
        if(activityClass!= null){
            Intent intent = new Intent(getActivity(), activityClass);
            startActivity(intent);
//        finish();
        }

    }


    //初始化数据
    private void initData() {

        listViewData.add("我的联系人");
        listViewData.add("我的账户");
        listViewData.add("我的密码");
        //通知ListView 重新渲染
        adapter.notifyDataSetChanged();


    }

    @Override
    public void onClick(View v) {
        //跳转到TicketActivity
        Intent intent = new Intent(getActivity(), TicketActivity.class);
        startActivity(intent);
    }

}